//
//  SEWebViewController.m
//  kuaiyi
//
//  Created by Zhu Jinxin on 16/7/7.
//  Copyright © 2016年 Alphts. All rights reserved.
//

/**
 *  //公益活动VC
 */

#import "SEWebViewController.h"

@interface SEWebViewController ()<UIWebViewDelegate>

@property(nonatomic,weak) UIWebView *myWebView;

@end

@implementation SEWebViewController

/*
 
 bug1:网络延迟的时候一直转圈,设置请求最长时间,在webview的didFailLoadWithError方法中让圆圈消失
 bug2:圆圈转到过程中,返回上一界面,转圈一直存在,无法消失-->viewWillDisappear中让圆圈消失
 bug3:用户体验,网络延迟,5秒未加载数据,则停止加载,提示网络状况不好
 */


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self login];
}


- (void)setupUI{
    
    UIWebView *myWebView = [[UIWebView alloc]init];
    
    _myWebView = myWebView;
    
    _myWebView.delegate = self;
    
    [self.view addSubview:_myWebView];
    
    [_myWebView makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.equalTo(self.view);
        
    }];
}

- (void)login{
#warning 循环引用 即使没有调用这个block
#warning 断网的条件下进入公益界面  然后再联网--->解决了
    __weak SEWebViewController *_weakSelf = self;
    
    [SENetworkReachabilityTool checkNetworkWithReachableBlock:^{
       
        NSString *urlString = @"http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm";
        
        NSURLRequest *res = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:0 timeoutInterval:5];
        
        [_weakSelf.myWebView loadRequest:res];
        
    }];

}


#pragma mark - delegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}


- (void)viewDidDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}

//请求时间超过设置的时间,执行这句代码
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [SVProgressHUD dismiss];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [SVProgressHUD showInfoWithStatus:@"网络连接超时"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
    });
    
}



@end
